//
//  KZMineVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/23.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

import RxCocoa
import RxSwift
import RxDataSources

class KZMineVC: KZBaseViewController {

    let bag = DisposeBag()
    
    let table = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 11.0, *) {
            
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        setupUI()
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setNavigationBackgroungAttributes(naviTranslucent: true)
    }
    
    override func viewSafeAreaInsetsDidChange() {
        if #available(iOS 11.0, *) {
            table.snp.updateConstraints { (m) in
                m.bottom.equalTo(-view.safeAreaInsets.bottom)
            }
        }
    }
}

// MARK: ------ 创建视图
extension KZMineVC {
    
    private func setupUI() {
        
        let header = KZMineUserInfoHeader(frame: CGRect(x: 0, y: 0, width: view.width, height: 220))
        table.tableHeaderView = header
        
        let footer = UIButton()
        footer.backgroundColor = UIColor.white
        footer.frame = CGRect(x: 0, y: 0, width: view.width, height: 40)
        footer.setTitle("退出登录", for: .normal)
        footer.setTitleColor(UIColor.black, for: .normal)
        footer.titleLabel?.font = kFont(15)
        table.tableFooterView = footer
        
        table.rx.contentOffset.map({
            return $0.y
        }).bind(to: header.rx.offset).disposed(by: bag)
        
        table.delegate = self
        table.backgroundColor = kBgColor
        table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(table)
        table.snp.makeConstraints { (m) in
            m.left.right.top.equalTo(0)
            m.bottom.equalTo(-49)
        }
        
        let items = Observable.just([
            SectionModel(model: "", items:
                [
                    "购物列表",
                    "消息通知",
                    "提现记录",
                ]),
            SectionModel(model: "", items:
                [
                    "反馈",
                    "清理缓存",
                    "检查更新",
                    "推荐给朋友",
                ])
            ])
        
        let dataSource = RxTableViewSectionedReloadDataSource
            <SectionModel<String, String>>(configureCell: {
                (dataSource, tableView, indexPath, element) in
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!

                cell.textLabel?.font = kFont(14)
                cell.textLabel?.text = element
                cell.imageView?.image = UIImage.init(named: "我的-\(element)")
                return cell
            })
        
        //绑定单元格数据
        items
            .bind(to: table.rx.items(dataSource: dataSource))
            .disposed(by: bag)
        
    }
}

// MARK: ------ Table delegate
extension KZMineVC: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.section == 0 {
            
            // 购物列表
            if indexPath.row == 0 {
                
                let vc =  KZShopingListVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            // 消息通知
            if indexPath.row == 1 {
                
                let vc = KZMessageVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            // 提现记录
            if indexPath.row == 2 {
                
                let vc = KZWithdrawRecordVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}
